/*
 * Copyright 2015 Alan.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.alanz.screencapture;

import com.melloware.jintellitype.JIntellitype;
import me.alanz.screencapture.log.LoggerUtil;
import me.alanz.screencapture.model.Setting;
import me.alanz.screencapture.tool.AlertTool;
import me.alanz.screencapture.tool.XMLTool;
import java.awt.Rectangle;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 *
 * @author Alan
 */
public class FXMain extends Application {

    private static final Logger logger = Logger.getLogger(FXMain.class.getName());
    private static Setting setting;

    public static Setting getSetting() {
        return setting;
    }

    private void loadSetting() {
        setting = XMLTool.readSetting();
        if (setting == null) {
            setting = new Setting();
            setting.setDefaultCaptureType("全屏截图");
            setting.setDirectory("C:/");
            setting.setFixedRectangle(new Rectangle(0, 0, 1024, 768));
        }
    }

    @Override
    public void start(Stage primaryStage) {
        LoggerUtil.config();

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                logger.log(Level.SEVERE, "", e);
                AlertTool.Alert(e.getMessage() + " in " + t.getName());
            }
        });

        loadSetting();

        StackPane root;
        try {
            URL url = getClass().getResource("/me/alanz/screencapture/view/MainFXML.fxml");
            root = (StackPane) FXMLLoader.load(url);
        } catch (IOException ex) {
            Logger.getLogger(FXMain.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }

        Scene scene = new Scene(root, root.getPrefWidth(), root.getPrefHeight());

        primaryStage.setTitle("屏幕截图工具");
        primaryStage.setScene(scene);
        primaryStage.show();

        primaryStage.setOnHiding((WindowEvent event) -> {
            XMLTool.saveSetting(setting);
            JIntellitype.getInstance().cleanUp();
        });
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
